package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.net.http.SslError;
import android.os.Build;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;

/**
 * 作者：Administrator on 2017/9/5 17:28
 * 邮箱：zhanghuaiha@gmail.com
 */

public class WebActivity extends BaseActivity {
    private WebView webView;
    private TextView add_title;
    private ImageView back;
    private String url;
    private WebSettings settings;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_web;
    }

    @Override
    public void initView() {
        webView = (WebView) findViewById(R.id.webView);
        add_title = (TextView) findViewById(R.id.add_title);
        back = (ImageView) findViewById(R.id.back);
        url = getIntent().getStringExtra("url");
    }

    @Override
    public void initData() {
        add_title.setText("游览器");
        settings = webView.getSettings();
//        //设置适应屏幕
//        settings.setUseWideViewPort(true);
//        settings.setLoadWithOverviewMode(true);
//        //设置缩放
//        settings.setSupportZoom(true);
        //关闭缓存
        settings.setAllowFileAccess(true);
        //设置支持JS
        settings.setJavaScriptEnabled(true);
//        //设置是否以http,https方式从网路加载图片资源
//        settings.setBlockNetworkImage(true);
//        //设置是否从网络加载资源
//        settings.setBlockNetworkLoads(true);
//        settings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    webView.getSettings()
                            .setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        webView.loadUrl(url);

    }

    @Override
    public void initListener() {
        back.setOnClickListener(v -> finish());
    }

}
